package com.pro.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pro.beans.Grid;
import com.pro.beans.GridPersons;
import com.pro.beans.News;
import com.pro.beans.Person;
import com.pro.dao.GridDao;
import com.pro.dao.GridPersonsDao;
import com.pro.dao.NewsDao;
import com.pro.dao.PersonDao;

@Service
public class GridService {
	
	@Resource
	public GridDao gridDao;
	
	@Resource
	public NewsDao newsDao;
	
	@Resource
	public GridPersonsDao gridPersonsDao;
	
	@Resource
	public PersonDao personDao;
	
	public void addGrid(Grid grid){
		gridDao.addObject(grid);
	}
	public void addPerson(Person person){
		personDao.addObject(person);
		GridPersons gridPersons = new GridPersons();
		gridPersons.setDate(new Date());
		gridPersons.setGrid_id(person.getGrid_id());
		gridPersons.setPerson_id(person.getId());
		gridPersons.setType(person.getType());
		gridPersonsDao.addObject(gridPersons);
	}
	
	public List<Grid> loadAllGrid(){
		List<Grid> gridList = gridDao.getAllObjects();
		return gridList;
	}
	
	public void delGrid(Integer id){
		Grid grid = new Grid();
		grid.setDel_flag(1);
		grid.setId(id);
		gridDao.updateObject(grid);
	}
	//网格内消防员
	public List<GridPersons> loadFirePersonInGrid(Integer gridId){
		GridPersons gridPersons = new GridPersons();
		gridPersons.setGrid_id(gridId);
		gridPersons.setType(0);
		return gridPersonsDao.getObjectsByObject(gridPersons);
	}
	
	//网格内管理员
	public List<GridPersons> loadDutyPersonInGrid(Integer gridId){
		GridPersons gridPersons = new GridPersons();
		gridPersons.setGrid_id(gridId);
		gridPersons.setType(1);
		return gridPersonsDao.getObjectsByObject(gridPersons);
	}
	public Grid loadGridById(Integer id){
		return gridDao.getObject(id);
	}
	public News loadNewById(Integer id){
		return newsDao.getObject(id);
	}
	public Map<Integer,List<News>> loadNews(){
		List<News> newList = newsDao.getAllObjects();
		Map<Integer,List<News>> map = new HashMap<Integer, List<News>>();
		List<News> newList1 = new ArrayList<News>();
		map.put(1, newList1);
		List<News> newList2 = new ArrayList<News>();
		map.put(2, newList2);
		List<News> newList3 = new ArrayList<News>();
		map.put(3, newList3);
		List<News> newList4 = new ArrayList<News>();
		map.put(4, newList4);
		List<News> newList5 = new ArrayList<News>();
		map.put(5, newList5);
		for(News news:newList){
			if(news.getType()!=null&&news.getType()==1){
				newList1.add(news);
			}
			if(news.getType()!=null&&news.getType()==2){
				newList2.add(news);
			}
			if(news.getType()!=null&&news.getType()==3){
				newList3.add(news);
			}
			if(news.getType()!=null&&news.getType()==4){
				newList4.add(news);
			}
			if(news.getType()!=null&&news.getType()==5){
				newList5.add(news);
			}
		}
		return map;
	}
	public void delPerson(Integer id){
		Person person = new Person();
		person.setDel_flag(1);
		person.setId(id);
		personDao.updateObject(person);
	}
}
